using Game.Component.Character;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class OperationBedItem : BaseItem
	{
		private enum State
		{
			Y_1 = 0,
			Y_2_3 = 1,
			Y_3 = 2,
			Y_2_1 = 3
		}

		private float Y_1 = 1.45f;

		private float Y_2 = 1.75f;

		private float Y_3 = 2.05f;

		private State state;

		private float curVal;

		private float targetVal;

		private bool isPlayAnim;

		private float timer;

		private BaseItem displayer;

		private BaseItem ecg;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		public override void ResetState()
		{
			base.ResetState();
			SetState((State)archiveData.state);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isPlayAnim)
			{
				timer += Time.deltaTime;
				model.SetLocalY(Mathf.Lerp(curVal, targetVal, timer));
				if (timer >= 1f)
				{
					isPlayAnim = false;
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && item.itemData.scriptType == 1)
			{
				if (displayer == null)
				{
					displayer = Singleton<Scene>.Instance.GetItemByScriptType(127);
				}
				if (displayer != null)
				{
					if (updatePosition)
					{
						((OperationDisplayerItem)displayer).SetRunningState();
					}
					else
					{
						((OperationDisplayerItem)displayer).OpenDisplayer();
					}
				}
				if (ecg == null)
				{
					ecg = Singleton<Scene>.Instance.GetItemByScriptType(173);
				}
				if (ecg != null)
				{
					((OperationECGItem)ecg).IsRunning(true);
				}
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (item.itemData.scriptType == 1)
			{
				((CharacterItem)item).ChangeFaceAnimation(Face.State.Happy);
				if (displayer == null)
				{
					displayer = Singleton<Scene>.Instance.GetItemByScriptType(127);
				}
				if (displayer != null)
				{
					((OperationDisplayerItem)displayer).CloseDisplayer();
				}
				if (ecg == null)
				{
					ecg = Singleton<Scene>.Instance.GetItemByScriptType(173);
				}
				if (ecg != null)
				{
					((OperationECGItem)ecg).IsRunning(false);
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (!isPlayAnim)
			{
				if (state == State.Y_1)
				{
					curVal = Y_1;
					targetVal = Y_2;
					state = State.Y_2_3;
				}
				else if (state == State.Y_2_3)
				{
					curVal = Y_2;
					targetVal = Y_3;
					state = State.Y_3;
				}
				else if (state == State.Y_3)
				{
					curVal = Y_3;
					targetVal = Y_2;
					state = State.Y_2_1;
				}
				else if (state == State.Y_2_1)
				{
					curVal = Y_2;
					targetVal = Y_1;
					state = State.Y_1;
				}
				Singleton<AudioManager>.Instance.PlaySound("sound_operation_bed_1");
				timer = 0f;
				isPlayAnim = true;
			}
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Y_1)
			{
				model.SetLocalY(Y_1);
			}
			else if (state == State.Y_3)
			{
				model.SetLocalY(Y_3);
			}
			else
			{
				model.SetLocalY(Y_2);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
